<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>模板的优化 - 欢迎来到SY的博客</title><meta name="description" content="模板的优化"><meta property="og:title" content="模板的优化" />
<meta property="og:description" content="模板的优化" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" /><meta property="og:image" content="https://acking-you.gitee.io/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2022-08-14T00:00:00+00:00" />
<meta property="article:modified_time" content="2022-08-14T00:00:00+00:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://acking-you.gitee.io/logo.png"/>

<meta name="twitter:title" content="模板的优化"/>
<meta name="twitter:description" content="模板的优化"/>
<meta name="application-name" content="FeelIt">
<meta name="apple-mobile-web-app-title" content="FeelIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="canonical" href="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" /><link rel="prev" href="https://acking-you.gitee.io/posts/%C3%A5%C3%A5%C3%A5%C3%A9%C3%A9/" /><link rel="next" href="https://acking-you.gitee.io/posts/final%C3%A5override/" /><link rel="stylesheet" href="/css/page.min.css"><link rel="stylesheet" href="/css/home.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "模板的优化",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/acking-you.gitee.io\/posts\/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5\/"
        },"genre": "posts","keywords": "模板的优化","wordcount":  1535 ,
        "url": "https:\/\/acking-you.gitee.io\/posts\/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5\/","datePublished": "2022-08-14T00:00:00+00:00","dateModified": "2022-08-14T00:00:00+00:00","publisher": {
            "@type": "Organization",
            "name": "作者"},"author": {
                "@type": "Person",
                "name": "作者"
            },"description": "模板的优化"
    }
    </script></head><body data-header-desktop="auto" data-header-mobile="auto"><script>(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="欢迎来到SY的博客">欢迎来到SY的博客</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="#" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="欢迎来到SY的博客">欢迎来到SY的博客</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="#" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="#" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><div class="menu-item"><a href="javascript:void(0);" class="theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div></div>
    </div>
</header><div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single" data-toc="disable"><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdnimg.cn/img_convert/e5259f24f6a5a7fb2331d3fd0d18e631.png#pic_center"
        data-srcset="https://img-blog.csdnimg.cn/img_convert/e5259f24f6a5a7fb2331d3fd0d18e631.png#pic_center, https://img-blog.csdnimg.cn/img_convert/e5259f24f6a5a7fb2331d3fd0d18e631.png#pic_center 1.5x, https://img-blog.csdnimg.cn/img_convert/e5259f24f6a5a7fb2331d3fd0d18e631.png#pic_center 2x"
        data-sizes="auto"
        alt="https://img-blog.csdnimg.cn/img_convert/e5259f24f6a5a7fb2331d3fd0d18e631.png#pic_center"
        title="模板的优化" /></div><div class="single-card" data-image="true"><h2 class="single-title animated flipInX">模板的优化</h2><div class="post-meta">
                <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>作者</a></span>&nbsp;<span class="post-category">出版于  <a href="/categories/c++11%E7%9A%84%E5%AD%A6%E4%B9%A0/"><i class="far fa-folder fa-fw"></i>C++11的学习</a></span></div>
                <div class="post-meta-line"><span><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2022-08-14">2022-08-14</time></span>&nbsp;<span><i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 1535 字</span>&nbsp;
                    <span><i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 4 分钟</span>&nbsp;</div>
            </div>
            
            <hr><div class="details toc" id="toc-static"  data-kept="">
                    <div class="details-summary toc-title">
                        <span>目录</span>
                        <span><i class="details-icon fas fa-angle-right"></i></span>
                    </div>
                    <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#1模板的右尖括号">1、模板的右尖括号</a></li>
    <li><a href="#2默认模板参数">2、默认模板参数</a></li>
  </ul>
</nav></div>
                </div><div class="content" id="content"><h2 id="1模板的右尖括号">1、模板的右尖括号</h2>
<p>在泛型编程中，模板实例化有一个非常繁琐的地方，那就是连续的两个右尖括号(&raquo;)会被编译器解析成为右移操作符，而不是模板参数表的结束，我们先来看一段关于容器遍历的代码，在创建类模板<strong>Base</strong>中提供了遍历容器的操作函数<strong>traversal()：</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="c1">// test.cpp
</span><span class="c1"></span><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;vector&gt;</span><span class="cp">
</span><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">T</span><span class="o">&gt;</span>
<span class="k">class</span> <span class="nc">Base</span>
<span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
    <span class="kt">void</span> <span class="n">traversal</span><span class="p">(</span><span class="n">T</span><span class="o">&amp;</span> <span class="n">t</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">auto</span> <span class="n">it</span> <span class="o">=</span> <span class="n">t</span><span class="p">.</span><span class="n">begin</span><span class="p">();</span>
        <span class="k">for</span> <span class="p">(;</span> <span class="n">it</span> <span class="o">!=</span> <span class="n">t</span><span class="p">.</span><span class="n">end</span><span class="p">();</span> <span class="o">++</span><span class="n">it</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="o">*</span><span class="n">it</span> <span class="o">&lt;&lt;</span> <span class="s">&#34; &#34;</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">};</span>


<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v</span><span class="p">{</span> <span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">9</span> <span class="p">};</span>
    <span class="n">Base</span><span class="o">&lt;</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;&gt;</span> <span class="n">b</span><span class="p">;</span>
    <span class="n">b</span><span class="p">.</span><span class="n">traversal</span><span class="p">(</span><span class="n">v</span><span class="p">);</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>结果如下：</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141616740.png"
        data-srcset="https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141616740.png, https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141616740.png 1.5x, https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141616740.png 2x"
        data-sizes="auto"
        alt="https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141616740.png"
        title="image-20220814161613635" /></p>
<p>如果我们使用<strong>C++98/03</strong>标准来编译上边的这段代码，就会得到如下错误提示：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">源.cpp:25:20: error: <span class="s1">&#39;&gt;&gt;&#39;</span> should be <span class="s1">&#39;&gt; &gt;&#39;</span> within a nested template argument list
     Base&lt;vector&lt;int&gt;&gt; b<span class="p">;</span>
</code></pre></div><p>根据错误提示中描述模板的两个右尖括号之间需要添加空格，这样写起来就非常麻烦，<strong>C++11改进了编译器的解析规则，尽可能地将多个右尖括号(&gt;)解析成模板参数结束符</strong>，方便我们编写模板相关的代码。</p>
<p>上面这段代码，在支持<strong>C++11</strong>编译器中编译是没有任何问题的，如果使用g++直接编译需要添加参数**-std=c++11：**</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell">$ g++ test.cpp -std<span class="o">=</span>c++11 -o app
</code></pre></div><h2 id="2默认模板参数">2、默认模板参数</h2>
<p>在<strong>C++98/03</strong>标准中，类模板可有有默认的模板参数：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">T</span><span class="o">=</span><span class="kt">int</span><span class="p">,</span> <span class="n">T</span> <span class="n">t</span><span class="o">=</span><span class="mi">520</span><span class="o">&gt;</span>
<span class="k">class</span> <span class="nc">Test</span>
<span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
    <span class="kt">void</span> <span class="n">print</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;current value: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">t</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">Test</span><span class="o">&lt;&gt;</span> <span class="n">t</span><span class="p">;</span>
    <span class="n">t</span><span class="p">.</span><span class="n">print</span><span class="p">();</span>

    <span class="n">Test</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="mi">1024</span><span class="o">&gt;</span> <span class="n">t1</span><span class="p">;</span>
    <span class="n">t1</span><span class="p">.</span><span class="n">print</span><span class="p">();</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>结果如下：</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141625623.png"
        data-srcset="https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141625623.png, https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141625623.png 1.5x, https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141625623.png 2x"
        data-sizes="auto"
        alt="https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141625623.png"
        title="image-20220814162522571" /></p>
<p>但是不支持函数默认的模板参数，<strong>在C++11中添加了对函数模板默认参数的支持：</strong></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">T</span><span class="o">=</span><span class="kt">int</span><span class="o">&gt;</span>	<span class="c1">// C++98/03不支持这种写法, C++11中支持这种写法
</span><span class="c1"></span><span class="kt">void</span> <span class="n">func</span><span class="p">(</span><span class="n">T</span> <span class="n">t</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;current value: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">t</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">func</span><span class="p">(</span><span class="mi">100</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>通过上面的例子可以得到如下结论：<strong>当所有模板参数都有默认参数时，函数模板的调用如同一个普通函数。但对于类模板而言，哪怕所有参数都有默认参数，在使用时也必须在模板名后跟随&lt;&gt;来实例化。</strong></p>
<p>**另外：函数模板的默认模板参数在使用规则上和其他的默认参数也有一些不同，它没有必须写在参数表最后的限制。**这样当默认模板参数和模板参数自动推导结合起来时，书写就显得非常灵活了。我们可以指定函数模板中的一部分模板参数使用默认参数，另一部分使用自动推导，比如下面的例子：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp">
</span><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">R</span> <span class="o">=</span> <span class="kt">int</span><span class="p">,</span> <span class="k">typename</span> <span class="n">N</span><span class="o">&gt;</span>
<span class="n">R</span> <span class="n">func</span><span class="p">(</span><span class="n">N</span> <span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">arg</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">auto</span> <span class="n">ret1</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="mi">520</span><span class="p">);</span>
    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;return value-1: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">ret1</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>

    <span class="k">auto</span> <span class="n">ret2</span> <span class="o">=</span> <span class="n">func</span><span class="o">&lt;</span><span class="kt">double</span><span class="o">&gt;</span><span class="p">(</span><span class="mf">52.134</span><span class="p">);</span>
    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;return value-2: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">ret2</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>

    <span class="k">auto</span> <span class="n">ret3</span> <span class="o">=</span> <span class="n">func</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="mf">52.134</span><span class="p">);</span>
    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;return value-3: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">ret3</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>

    <span class="k">auto</span> <span class="n">ret4</span> <span class="o">=</span> <span class="n">func</span><span class="o">&lt;</span><span class="kt">char</span><span class="p">,</span> <span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">100</span><span class="p">);</span>
    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;return value-4: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">ret4</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>结果如下：</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141723474.png"
        data-srcset="https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141723474.png, https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141723474.png 1.5x, https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141723474.png 2x"
        data-sizes="auto"
        alt="https://raw.githubusercontent.com/jjking20010901/typora/master/img202208141723474.png"
        title="image-20220814172321414" /></p>
<p>根据得到的输出结果，分析以下示例代码中调用的模板函数：</p>
<ul>
<li>
<p><strong>auto ret1= func(520);</strong></p>
<p>函数返回值类型使用了默认的模板参数，函数的参数类型自动推导出来为<strong>int</strong>类型。</p>
</li>
<li>
<p><strong>auto ret2= func<!-- raw HTML omitted -->(52.134);</strong></p>
<p>函数的返回值指定为<strong>double</strong>类型，函数参数是通过实参推导出来的，为<strong>double</strong>类型。</p>
</li>
<li>
<p><strong>auto ret3= func<!-- raw HTML omitted -->(52.134);</strong></p>
<p>函数的返回值指定为<strong>int</strong>类型，函数的参数是通过实参推导出来的，为<strong>double</strong>类型。</p>
</li>
<li>
<p><strong>auto ret4= func&lt;char,int&gt;(100);</strong></p>
<p>函数的参数类型为<strong>int</strong>类型，函数的返回值指定为<strong>char</strong>类型，不需要推导。</p>
</li>
</ul>
<p>当默认模板参数和模板参数自动推导同时使用时(优先级从高到低)：</p>
<ul>
<li><strong>如果可以推导出参数类型则使用推导出的类型。</strong></li>
<li><strong>如果函数模板无法推导出参数类型，那么编译器会使用默认模板参数。</strong></li>
<li><strong>如果无法推导出模板参数类型并且没有设置默认模板参数，编译器就会报错。</strong></li>
</ul>
<p>看一下下面的例子：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span><span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp">
</span><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="c1">// 函数模板定义
</span><span class="c1"></span><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">T</span><span class="p">,</span> <span class="k">typename</span> <span class="n">U</span> <span class="o">=</span> <span class="kt">char</span><span class="o">&gt;</span>
<span class="kt">void</span> <span class="n">func</span><span class="p">(</span><span class="n">T</span> <span class="n">arg1</span> <span class="o">=</span> <span class="mi">100</span><span class="p">,</span> <span class="n">U</span> <span class="n">arg2</span> <span class="o">=</span> <span class="mi">100</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;arg1: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">arg1</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;, arg2: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">arg2</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="c1">// 模板函数调用
</span><span class="c1"></span>    <span class="n">func</span><span class="p">(</span><span class="sc">&#39;a&#39;</span><span class="p">);</span>
    <span class="n">func</span><span class="p">(</span><span class="mi">97</span><span class="p">,</span> <span class="sc">&#39;a&#39;</span><span class="p">);</span>
    <span class="c1">// func();    //编译报错
</span><span class="c1"></span>    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><p>分析以下调用的模板函数func()：</p>
<ul>
<li>func(&lsquo;a&rsquo;)：参数T被自动推导为char类型，U使用默认的模板参数为char类型。</li>
<li>func(97,&lsquo;a&rsquo;)：参数T被自动推导为int类型，U使用推导出的char类型。</li>
<li>func()：参数T没有指定默认的模板类型，并且无法自动推导，编译器会报错。
<ul>
<li>模板参数类型的自动推导是根据模板函数调用时指定的实参进行推断的，没有实参无法推导。</li>
<li>模板参数类型的自动推导不会参考函数模板中指定的默认参数。</li>
</ul>
</li>
</ul>
</div><div class="post-footer" id="post-footer">
    <div class="post-info"><div class="post-info-tag"><span><a href="/tags/%E6%A8%A1%E6%9D%BF%E7%9A%84%E4%BC%98%E5%8C%96/">模板的优化</a>
                </span></div><div class="post-info-line"><div class="post-info-mod">
                <span>更新于 2022-08-14</span>
            </div><div class="post-info-mod"></div>
        </div><div class="post-info-share">
            <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" data-title="模板的优化" data-hashtags="模板的优化"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" data-hashtag="模板的优化"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 WhatsApp" data-sharer="whatsapp" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" data-title="模板的优化" data-web><i class="fab fa-whatsapp fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" data-title="模板的优化"><i class="fab fa-line fa-fw"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" data-title="模板的优化" data-image="https://img-blog.csdnimg.cn/img_convert/e5259f24f6a5a7fb2331d3fd0d18e631.png#pic_center"><i class="fab fa-weibo fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Myspace" data-sharer="myspace" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" data-title="模板的优化" data-description="模板的优化"><i data-svg-src="/lib/simple-icons/icons/myspace.min.svg"></i></a><a href="javascript:void(0);" title="分享到 Blogger" data-sharer="blogger" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" data-title="模板的优化" data-description="模板的优化"><i class="fab fa-blogger fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Evernote" data-sharer="evernote" data-url="https://acking-you.gitee.io/posts/%C3%A6%C3%A6%C3%A7%C3%A4%C3%A5/" data-title="模板的优化"><i class="fab fa-evernote fa-fw"></i></a></span>
        </div></div><div class="post-nav"><a href="/posts/%C3%A5%C3%A5%C3%A5%C3%A9%C3%A9/" class="prev" rel="prev" title="原始字面量"><i class="fas fa-angle-left fa-fw"></i>Previous Post</a>
            <a href="/posts/final%C3%A5override/" class="next" rel="next" title="final和override">Next Post<i class="fas fa-angle-right fa-fw"></i></a></div></div>
</div></article></div>
            </main>
            <footer class="footer"><div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.86.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/khusika/FeelIt" target="_blank" rel="noopener noreffer" title="FeelIt 1.0.1"><i class="fas fa-hand-holding-heart fa-fw"></i> FeelIt</a>
        </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2022</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/"></a></span></div>
</div>
</footer>
        </div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-chevron-up fa-fw"></i>
            </a></div><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><link rel="stylesheet" href="/lib/katex/katex.min.css"><link rel="stylesheet" href="/lib/katex/copy-tex.min.css"><script src="/lib/autocomplete/autocomplete.min.js"></script><script src="/lib/lunr/lunr.min.js"></script><script src="/lib/lunr/lunr.stemmer.support.min.js"></script><script src="/lib/lunr/lunr.zh.min.js"></script><script src="/lib/lazysizes/lazysizes.min.js"></script><script src="/lib/clipboard/clipboard.min.js"></script><script src="/lib/sharer/sharer.min.js"></script><script src="/lib/katex/katex.min.js"></script><script src="/lib/katex/auto-render.min.js"></script><script src="/lib/katex/copy-tex.min.js"></script><script src="/lib/katex/mhchem.min.js"></script><script>window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":200},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":100,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script src="/js/theme.min.js"></script></body></html>
